package com.example.springbootdynamicdatasource.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootdynamicdatasource.entity.LightUser;
import com.example.springbootdynamicdatasource.entity.User;
import com.example.springbootdynamicdatasource.mapper.LightUserMapper;
import com.example.springbootdynamicdatasource.mapper.UserMapper;
import com.example.springbootdynamicdatasource.service.LightUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author vicente
 * @since 2022-01-20
 */
@Service
public class LightUserServiceImpl extends ServiceImpl<LightUserMapper, LightUser> implements LightUserService {

    @Autowired
    private LightUserMapper lightUserMapper;

    @Override
    //@DS("db2")
    @Slave
    public List<LightUser> getUserList() {
        //QueryWrapper<User> last = new QueryWrapper<User>().eq("name", "Tom").eq("age", "28").last("limit 1");
        QueryWrapper<LightUser> userQueryWrapper = new QueryWrapper<>();
        List<LightUser> userList = lightUserMapper.selectList(userQueryWrapper);
        for (LightUser user: userList){
            System.out.println(JSON.toJSONString(user));
        }
        return userList;
    }

    @Override
    @Slave
    public LightUser getUserByName(String userName) {
        QueryWrapper<LightUser> eq = new QueryWrapper<LightUser>().eq("user_name", userName);
        LightUser lightUser = lightUserMapper.selectOne(eq);
        return lightUser;
    }

}
